package edu.xlh.travel.dao;

import edu.xlh.travel.pojo.Orders;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

public interface OrdersDao extends JpaRepository<Orders,String>, JpaSpecificationExecutor<Orders> {
    List<Orders> findByUserId(String userId);

    @Transactional
    @Modifying
    @Query(value = "update Orders set status =?1 where id =?2")
    void updateStatusByOrderId(@Param("status") String status , @Param("id") String id);

    @Modifying
    @Query(value = "update t_scenic set stock =?2 " +
            "where id = (" +
            "select a.scenic_id " +
            "from (" +
            "select o.scenic_id " +
            "from t_orders o " +
            "join t_scenic s " +
            "on o.scenic_id = s.id where o.id =?1) a )", nativeQuery=true)
    void updateStockByOrderId(String orderId, int stock);

    @Modifying
    @Query(value = "update t_orders set status =:#{#order.status} where id =:#{#order.id}",nativeQuery = true )
    void updateOrderById(@Param("order")Orders order);

    @Modifying
    @Query(value = "update t_orders set status =:#{#order.status} , paytime=:#{#order.paytime} , modify_time=:#{#order.modifyTime} where id =:#{#order.id}",nativeQuery = true )
    void updateOrderByEntitiy(Orders order);

    @Transactional
    @Modifying
    @Query(value = "update Orders set remarks =?2 where id =?1")
    void saveRemarks(@Param("id")String id, @Param("remarks") String remarks);

    int countByUserId(String userId);

    @Transactional
    @Modifying
    @Query(value = "update Orders set delFlag =?2 where id =?1")
    void updateOrderDelFlagByOrderId(String orderId, String delFlag);
}
